package com.test.restaurant.consumer.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.test.restaurant.pojo.Cuisine;
import com.test.restaurant.pojo.DishTable;
import com.test.restaurant.service.CuisineService;
import com.test.restaurant.service.DishTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * @author 徒有琴
 */
@Controller
public class IndexController {
    @Reference
    private DishTableService dishTableService;
    @Reference
    private CuisineService cuisineService;

    @RequestMapping("index.html")
    public String index(Model model) {
        List<DishTable> tables = dishTableService.getTableList(null);
        model.addAttribute("tables", tables);
        return "index";
    }

    @Autowired
    private RedisTemplate redisTemplate;

    @RequestMapping("dish_list.html")
    public String dish(Integer tid, HttpServletResponse response, Model model) {
        List<Cuisine> cuisines = (List<Cuisine>) redisTemplate.opsForValue().get("cuisines");
//        if (cuisines == null) {
//            //查库
//            cuisines = cuisineService.getCuisineList();
//            redisTemplate.opsForValue().set("cuisines", cuisines);
//            System.out.println("查库");
//        }
        Cookie cookie = new Cookie("table_id", tid.toString());
        response.addCookie(cookie);
        model.addAttribute("cuisines", cuisines);
        return "dishes";
    }
}
